package com.premadefood.filter;

import com.premadefood.common.ErrorCode;
import com.premadefood.domain.User;
import com.premadefood.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.premadefood.constant.UserConstant.USER_LOGIN_STATE;

@WebFilter
@Slf4j
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        User user = (User)request.getSession().getAttribute(USER_LOGIN_STATE);
        if(user == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"用户未登录");
        }
        filterChain.doFilter(request,response);
    }
}
